package com.yocard.yxyd.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.widget.TextView;

import com.yocard.yxyd.R;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created on 2019/5/27.
 * 跟进记录标签适配器
 */
public class FollowTagsAadapter extends RecyclerView.Adapter<FollowTagsAadapter.ViewHolder> {

    private int clickPos = -1;
    private List<String> tagList;
    private ValueCallback<String> callback;

    public FollowTagsAadapter(List<String> tagList) {

        this.tagList = tagList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_follow_tag, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        if (i == clickPos) {
            viewHolder.tv_follow_tag.setSelected(true);
        } else {
            viewHolder.tv_follow_tag.setSelected(false);
        }

        String itemBean = tagList.get(i);
        viewHolder.tv_follow_tag.setText(itemBean);
        viewHolder.tv_follow_tag.setTag(i);
        viewHolder.tv_follow_tag.setOnClickListener(clickListener);
    }

    @Override
    public int getItemCount() {
        return tagList == null ? 0 : tagList.size();
    }

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            clickPos = (int) v.getTag();
            notifyDataSetChanged();

            if(callback != null){
                callback.onReceiveValue(getSelectedAmount());
            }
        }
    };

    public void setOnItemclick(ValueCallback<String> callback){

        this.callback = callback;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_follow_tag)
        TextView tv_follow_tag;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public String getSelectedAmount(){
        String amount = "";
        if(tagList.size() >0){
            amount = tagList.get(clickPos);
        }
        return amount;
    }

}
